<template>
  <a-form class="smart-query-form" ref="queryFormRef">
    <a-row class="smart-query-form-row">
      <a-form-item label="上游标识" class="smart-query-form-item">
        <a-input style="width: 300px" v-model:value="queryForm.upstreamCode" placeholder="上游标识" />
      </a-form-item>

      <a-form-item class="smart-query-form-item smart-margin-left10">
        <a-button-group>
          <a-button type="primary" @click="onSearch">
            <template #icon>
              <SearchOutlined />
            </template>
            查询
          </a-button>
          <a-button @click="resetQuery">
            <template #icon>
              <ReloadOutlined />
            </template>
            重置
          </a-button>
        </a-button-group>
      </a-form-item>
    </a-row>
  </a-form>

  <a-card size="small" :bordered="false">
    <a-row justify="end" ref="tableOperatorRef">
      <TableOperator class="smart-margin-bottom5" v-model="columns" :tableId="TABLE_ID_CONST.UPSTREAM.BOX" :refresh="ajaxQuery" />
    </a-row>
    <a-table
      size="small"
      :dataSource="tableData"
      :columns="columns"
      bordered
      rowKey="id"
      :pagination="false"
      :loading="tableLoading"
      :scroll="{ y: scrollY }"
    >
      <template #bodyCell="{ text, record, column }">
        <template v-if="column.dataIndex === 'upstreamId'">
          <span :class="'text-copy-upstreamId-' + record.id" :data-clipboard-text="text" @click="textCopy('upstreamId-' + record.id)">{{ text }}</span>
        </template>
        <template v-else-if="column.dataIndex === 'upstreamCode'">
          <span>{{ $smartEnumPlugin.getDescByValue('UPSTREAM_ENUM', text) }}</span>
        </template>
        <template v-else-if="column.dataIndex === 'onlineFlag'">
          <template v-if="text">
            <a-tag color="success">在线</a-tag>
          </template>
          <template v-else>
            <a-tag color="error">离线</a-tag>
          </template>
        </template>
        <template v-else-if="column.dataIndex === 'provinces'">
          <span>{{ (record?.province || '') + record?.city }}</span>
        </template>
        <template v-else-if="column.dataIndex === 'operate'">
          <div class="smart-table-operate">
            <a-button type="link" size="small">详情</a-button>
          </div>
        </template>
      </template>
    </a-table>

    <div class="smart-query-table-page">
      <a-pagination
        showSizeChanger
        showQuickJumper
        show-less-items
        :pageSizeOptions="PAGE_SIZE_OPTIONS"
        :defaultPageSize="queryForm.pageSize"
        v-model:current="queryForm.pageNum"
        v-model:pageSize="queryForm.pageSize"
        :total="total"
        @change="ajaxQuery"
        @showSizeChange="ajaxQuery"
        :show-total="(total) => `共${total}条`"
      />
    </div>
  </a-card>
</template>
<script setup>
  import { onMounted, onUnmounted, reactive, ref } from 'vue';
  import { PAGE_SIZE_OPTIONS } from '/@/constants/common-const';
  import { upstreamBoxApi } from '/@/api/upstream/box/upstream-box-api';
  import { smartSentry } from '/@/lib/smart-sentry';
  import TableOperator from '/@/components/support/table-operator/index.vue';
  import { TABLE_ID_CONST } from '/@/constants/support/table-id-const';
  import { calcTableHeight } from '/@/lib/table-auto-height';
  import { message } from 'ant-design-vue';
  import Clipboard from 'clipboard';

  const columns = ref([
    {
      title: '上游编号',
      dataIndex: 'upstreamId',
      ellipsis: true,
      width: 200,
    },
    {
      title: '上游',
      dataIndex: 'upstreamCode',
      width: 80,
    },
    {
      title: '用户',
      dataIndex: 'nickName',
      width: 85,
    },
    {
      title: '企业',
      dataIndex: 'enterpriseName',
      width: 130,
    },
    {
      title: '产品',
      dataIndex: 'productName',
      width: 100,
    },
    {
      title: '价格',
      dataIndex: 'productPrice',
      width: 80,
    },
    {
      title: 'nat类型',
      dataIndex: 'natTypeName',
      width: 75,
    },
    {
      title: '业务状态',
      dataIndex: 'serviceStatus',
      width: 110,
    },
    {
      title: '在线状态',
      dataIndex: 'onlineFlag',
      width: 80,
    },
    {
      title: '审核状态',
      dataIndex: 'reviewStatus',
      width: 80,
    },
    {
      title: '审核时间',
      dataIndex: 'reviewTime',
      width: 130,
    },
    {
      title: '运营商',
      dataIndex: 'isp',
      width: 60,
    },
    {
      title: '省市',
      dataIndex: 'provinces',
      width: 130,
    },
    {
      title: '备注',
      dataIndex: 'remark',
      ellipsis: true,
      width: 150,
    },
    {
      title: '创建时间',
      dataIndex: 'createTime',
      width: 150,
    },
    {
      title: '更新时间',
      dataIndex: 'updateTime',
      width: 150,
    },
    {
      title: '操作',
      dataIndex: 'operate',
      fixed: 'right',
      width: 250,
    },
  ]);

  const queryFormState = {
    upstreamCode: undefined,
    pageNum: 1,
    pageSize: 10,
  };
  const queryForm = reactive({ ...queryFormState });
  const createDateRange = ref([]);

  const tableLoading = ref(false);
  const tableData = ref([]);
  const total = ref(0);

  function resetQuery() {
    Object.assign(queryForm, queryFormState);
    createDateRange.value = [];
    ajaxQuery();
  }

  function onSearch() {
    queryForm.pageNum = 1;
    ajaxQuery();
  }

  async function ajaxQuery() {
    try {
      tableLoading.value = true;
      let responseModel = await upstreamBoxApi.queryList(queryForm);

      const list = responseModel.data.list;
      total.value = responseModel.data.total;
      tableData.value = list;
    } catch (e) {
      smartSentry.captureError(e);
    } finally {
      tableLoading.value = false;
    }
  }

  // 复制文本
  function textCopy(clzzName) {
    let clipboard = new Clipboard('.text-copy-' + clzzName);
    clipboard.on('success', (e) => {
      message.success('复制成功');
      //  释放内存
      clipboard.destroy();
    });
    clipboard.on('error', (e) => {
      // 不支持复制
      message.error('浏览器不支持复制，请您手动选择复制');
      // 释放内存
      clipboard.destroy();
    });
  }

  // ----------------- 表格自适应高度 --------------------
  const scrollY = ref(100);
  const tableOperatorRef = ref();
  const queryFormRef = ref();

  function autoCalcTableHeight() {
    calcTableHeight(scrollY, [tableOperatorRef, queryFormRef], 10);
  }

  window.addEventListener('resize', autoCalcTableHeight);

  onMounted(() => {
    ajaxQuery();
    autoCalcTableHeight();
  });

  onUnmounted(() => {
    window.removeEventListener('resize', autoCalcTableHeight);
  });
</script>
